﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using volva.Models;

namespace volva.Services
{
    public class AccountService
    {
        /// <summary>
        /// This function is used when registering new user.
        /// </summary>
        /// <param name="model">Information about user.</param>
        static public void Register(WebUser model)
        {
             DataService.ddc.WebUsers.InsertOnSubmit(model);
             DataService.ddc.SubmitChanges();
        }
        /// <summary>
        /// This function is used when a user wants to change his account, information etc.
        /// </summary>
        /// <param name="model">Information about user.</param>
        static public void ChangeAccount(WebUser model, int userId)
        {
            WebUser user = AccountService.GetUserByID(userId);

            user.address = model.address;
            user.areaCode = model.areaCode;
            user.email = model.email;
            user.nsfw = model.nsfw;
            user.sex = model.sex;

            DataService.ddc.SubmitChanges();
        }
        /// <summary>
        /// This function changes user's account, for example to TV-manager or schedule producer.
        /// </summary>
        /// <param name="accountID">Information about which user to update.</param>
        /// <param name="stationID">Which station the user will be TV-manager or schedule producer for.</param>
        /// <param name="role">What role the user will get.</param>
        /// <param name="confirmed">If the position is confirmed or not</param>
        static public void AddRole(WebUserRole userRole)
        {
            DataService.ddc.WebUserRoles.InsertOnSubmit(userRole);
            DataService.ddc.SubmitChanges();
        }

        static public void RemoveRole(WebUserRole userRole)
        {
            DataService.ddc.WebUserRoles.DeleteOnSubmit(userRole);
            DataService.ddc.SubmitChanges();
        }

        /// <summary>
        /// This function deletes a user's account.
        /// </summary>
        /// <param name="accountID">Which user should be deleted.</param>
        static public void DeleteAccount(int accountID)
        {
            WebUser user = AccountService.GetUserByID(accountID);

            DataService.ddc.WebUsers.DeleteOnSubmit(user);
            DataService.ddc.SubmitChanges();
        }
        static public WebUser GetUserByID(int userID)
        {
            return
                (from u in DataService.ddc.WebUsers
                 where u.id == userID
                 select u).SingleOrDefault();
        }
        static public int GetUserIDByName(string name)
        {
            return
                (from u in DataService.ddc.WebUsers
                 where u.username == name
                 select u.id).SingleOrDefault();
        }
    }
}
